<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="金融机构编码">
              <a-input placeholder="请输入金融机构编码" v-model="queryParam.bankcode"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="金融机构名称">
              <a-input placeholder="请输入金融机构名称" v-model="queryParam.financename"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <!--
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
-->
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus" v-has="'juridicalPerson:add'">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXlsJ('法人表')" v-has="'juridicalPerson:export'"
        >导出</a-button
      >
      <a-upload
        name="file"
        :showUploadList="false"
        :multiple="false"
        v-has="'juridicalPerson:import'"
        :headers="tokenHeader"
        :action="importExcelUrl"
        @change="handleImportExcel"
      >
        <!--<a-button type="primary" icon="import">导入</a-button>-->
      </a-upload>
      <!-- 高级查询区域 -->
      <!--<j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>-->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay" v-has="'juridicalPerson:batchDel'">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete" />删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <!--<div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>-->

      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="innercode" slot-scope="text">
          <div v-html="(innercodeList && innercodeList.length > 0 && filterLabel(text, innercodeList)) || text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt=""
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)" v-has="'juridicalPerson:edit'">编辑</a>
          <!--
          <a @click="showModalMechanism(record)" :style="{ marginLeft: '15px' }" v-has="'juridicalPerson:edit'"
            >机构配置</a
          >
          -->
          <a @click="showModalRule(record)" :style="{ marginLeft: '15px' }" v-has="'juridicalPerson:reportRule'"
            >报表规则配置</a
          >
          <a @click="showModalMessage(record)" :style="{ marginLeft: '15px' }" v-has="'juridicalPerson:messageRule'"
            >报文规则配置</a
          >

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm
                  title="确定删除吗?"
                  v-has="'juridicalPerson:batchDel'"
                  @confirm="() => handleDelete(record.id)"
                >
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
    </div>

    <llbb-juridical-person-modal ref="modalForm" @ok="modalFormOk"></llbb-juridical-person-modal>
    <llbb-juridical-person-mechanism-form
      @cancel="
        visibleMechanism = false
        detailTree = {}
      "
      @ok="handleSubmitMechanism"
      :visible="visibleMechanism"
      :detail="detailTree"
    ></llbb-juridical-person-mechanism-form>
    <llbb-juridical-person-rule-form
      @cancel="
        visibleRule = false
        detail = {}
      "
      @ok="handleSubmitRule"
      :visible="visibleRule"
      :detail="detail"
    ></llbb-juridical-person-rule-form>
    <llbb-juridical-person-message-form
      @cancel="
        visibleMessage = false
        detailMessage = {}
      "
      @ok="handleSubmitMessage"
      :visible="visibleMessage"
      :detail="detailMessage"
    ></llbb-juridical-person-message-form>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import LlbbJuridicalPersonModal from './modules/LlbbJuridicalPersonModal'
import LlbbJuridicalPersonMechanismForm from './modules/LlbbJuridicalPersonMechanismForm'
import LlbbJuridicalPersonRuleForm from './modules/LlbbJuridicalPersonRuleForm'
import LlbbJuridicalPersonMessageForm from './modules/LlbbJuridicalPersonMessageForm'
import { addDeploy, addAuth, addMessDeploy, getPraentDepartInfo } from '@/api/api'
import { downFile } from '@api/manage'

export default {
  name: 'LlbbJuridicalPersonList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    LlbbJuridicalPersonModal,
    LlbbJuridicalPersonMechanismForm,
    LlbbJuridicalPersonRuleForm,
    LlbbJuridicalPersonMessageForm,
  },
  data() {
    return {
      //配置详情
      detail: {},
      detailMessage: {},
      detailTree: {},
      //配置弹框状态
      visibleMechanism: false,
      visibleRule: false,
      visibleMessage: false,
      description: '法人表管理页面',
      // 表头
      columns: [
        {
          title: '选项',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },

        {
          title: '行内编码',
          align: 'center',
          dataIndex: 'innercode',
          scopedSlots: { customRender: 'innercode' },
        },
        {
          title: '金融机构编码',
          align: 'center',
          dataIndex: 'bankcode',
        },
        {
          title: '金融机构名称',
          align: 'center',
          dataIndex: 'financename',
        },
        {
          title: '法人名称',
          align: 'center',
          dataIndex: 'bankname',
        },
        {
          title: '法人标识',
          align: 'center',
          dataIndex: 'bankNoCd',
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/juridicalPersonManage/llbbJuridicalPerson/list',
        delete: '/juridicalPersonManage/llbbJuridicalPerson/delete',
        deleteBatch: '/juridicalPersonManage/llbbJuridicalPerson/deleteBatch',
        exportXlsUrl: '/juridicalPersonManage/llbbJuridicalPerson/exportXls',
        importExcelUrl: 'juridicalPersonManage/llbbJuridicalPerson/importExcel',
      },
      dictOptions: {},
      superFieldList: [],
      innercodeList: [],
    }
  },
  created() {
    this.getSuperFieldList()
    getPraentDepartInfo().then((res) => {
      if (res.success) {
        this.innercodeList = res.result.map((item, index, arr) => {
          let c = { label: item.DEPARTNAME, value: item.ORGCODE }
          return c
        })
      }
    })
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    filterLabel(value, list) {
      if (list.length && value) {
        let data = list.filter((item) => item.value === value)[0]
        return data ? data.label : ''
      }
      return ''
    },
    //打开机构配置弹框
    showModalMechanism(record) {
      this.detailTree = record
      this.visibleMechanism = true
    },
    handleExportXlsJ(){
      let param=new Object();
      let downloadUrl=this.url.exportXlsUrl;
      var that=this;
      that.fileName="法人表"
      downFile(downloadUrl,param).then((data)=>{
        if (!data) {
          that.$message.warning("文件下载失败")
          return
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data],{type: 'application/vnd.ms-excel'}), that.fileName+'.xlsx')
        }else{
          let url = window.URL.createObjectURL(new Blob([data],{type: 'application/vnd.ms-excel'}))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', that.fileName+'.xlsx')
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link); //下载完成移除元素
          window.URL.revokeObjectURL(url); //释放掉blob对象
        }
      });
    },
    //机构配置提交
    handleSubmitMechanism(values) {
      this.confirmLoading = true
      addDeploy(values).then((res) => {
        if (res.code === 200) {
          this.visibleMechanism = false
          this.detailTree = {}
          this.$message.success('保存成功')
        } else {
          this.$message.warning('服务器出错')
        }
        this.confirmLoading = false
      })
    },
    //打开报表规则配置弹框
    showModalRule(record) {
      this.detail = record
      this.visibleRule = true
    },
    //报表规则配置提交
    handleSubmitRule(values) {
      this.confirmLoading = true
      addAuth(values).then((res) => {
        if (res.code === 200) {
          this.visibleRule = false
          this.detail = {}
          this.$message.success('保存成功')
        } else {
          this.$message.warning('服务器出错')
        }
        this.confirmLoading = false
      })
    }, //打开报文规则配置弹框
    showModalMessage(record) {
      this.detailMessage = record
      this.visibleMessage = true
    },
    //报文规则配置提交
    handleSubmitMessage(values) {
      this.confirmLoading = true
      addMessDeploy(values).then((res) => {
        if (res.code === 200) {
          this.visibleMessage = false
          this.detailMessage = {}
          this.$message.success('保存成功')
        } else {
          this.$message.warning('服务器出错')
        }
        this.confirmLoading = false
      })
    },
    initDictConfig() {},
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'innercode', text: '行内编码' })
      fieldList.push({ type: 'string', value: 'bankcode', text: '金融机构编码' })
      fieldList.push({ type: 'string', value: 'financename', text: '金融机构名称' })
      fieldList.push({ type: 'string', value: 'bankname', text: '法人名称' })
      fieldList.push({ type: 'string', value: 'bankNoCd', text: '法人编号' })
      this.superFieldList = fieldList
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>